package com.isunland.managesystem.ui;

import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.adapter.FormChildAdapter;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.entity.FormChildDetail;
import com.isunland.managesystem.entity.FormDetail;
import java.util.ArrayList;

public class FormChildFragment extends Fragment {
  public static final String EXTRA_CHILD_LIST =
      "com.isunland.managesystem.Fragment.EXTRA_CHILD_DETAL";
  private static final String TAG = "FormChildFragment";
  private ArrayList<FormDetail> mList;
  private ListView mListView;
  private ArrayList<FormDetail> mTitles;

  public static FormChildFragment newInstance(ArrayList<ArrayList<FormChildDetail>> childList) {
    FormChildFragment fragment = new FormChildFragment();
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_CHILD_LIST, childList);
    fragment.setArguments(args);
    return fragment;
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_form_child_detail, container, false);
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.child_form_detail);
    mListView = (ListView) view.findViewById(R.id.lv_child_form_detail);
    FormChildAdapter adapter = new FormChildAdapter(getActivity(), mList, mTitles);
    mListView.setAdapter(adapter);
    return view;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
      if (NavUtils.getParentActivityName(getActivity()) != null) {

        ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
      }
    }
    ArrayList<ArrayList<FormChildDetail>> childList =
        (ArrayList<ArrayList<FormChildDetail>>) getArguments().getSerializable(EXTRA_CHILD_LIST);
    mList = new ArrayList<FormDetail>();
    mTitles = new ArrayList<FormDetail>();
    for (ArrayList<FormChildDetail> arrayList : childList) {
      for (FormChildDetail formChildDetail : arrayList) {
        String name = "";
        String value = "";

        name = formChildDetail.getName();
        value = formChildDetail.getValue();
        name = name.replace(FormChildListFragment.SHOW_MARK, "");
        value = value.replace(FormChildListFragment.SHOW_MARK, "");
        if (arrayList.get(0).equals(formChildDetail)) {

          mTitles.add(new FormDetail(name, value));
        }

        mList.add(new FormDetail(name, value));
      }
    }
    super.onCreate(savedInstanceState);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:

        getActivity().finish();
        return true;
      default:
        return super.onOptionsItemSelected(item);
    }
  }
}
